package ex.algorithms.heap;

import java.util.Arrays;

import ex.algorithms.sorting.HeapSort;
import ex.util.CommonUtil;

public class PriorityQueue {
	public static int maximum(int[] input) {
		if(input == null || input.length < 1)
			return 0;
		
		return input[0];
	}
	
	public static int extractMaximum(int[] input) {
		if(input == null || input.length < 1)
			return 0;
		
		int max = input[0];
		input[0] = input[input.length - 1];
		Heap.maxHeapify(input, 1, input.length - 1);
		return max;
	}
	
	public static void increaseKey(int[] input, int key, int newValue) {
		if(input == null || input.length < 1)
			return ;
		
		input[key - 1] = newValue;
		while(key > 1) {
			int parent = key/2;
			if(input[key -1] > input[parent - 1]) {
				CommonUtil.swap(input, key-1, parent-1);
				key = parent;
			}
			else 
				break;
		}
	}
	
	public static int[] insert(int[] input, int newKey) {
		int[] newArray = Arrays.copyOf(input, input.length + 1);
		newArray[newArray.length - 1] = Integer.MIN_VALUE;
		increaseKey(newArray, newArray.length, newKey);
		return newArray;
	}
}
